package de.unimannheim.ki.usermanagement;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import de.unimannheim.ki.servlets.WebServiceClient;

public class LastFMAPIHandler {

	static String APIKEY = "7641e28f7cdec04671e0c3c19455dc8f";
	private Gson gson;
	
	public LastFMAPIHandler() {
		gson = new Gson();
	}
	
	
	public void getUserLovedTracks(){
		
	}
	
	
	public void getUserRecentTracks(String userid, WebServiceClient wsClient){
		System.out.println("test");
		String url = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user="+userid+"&api_key="+APIKEY+"&format=json";
		// Replace by throwing an exception
		try {
				InputStream stream=  wsClient.perfomSynchCall(url);
				JsonParser parser = new JsonParser();
			  // get Json String
			  JsonObject obj = parser.parse(new InputStreamReader(stream)).getAsJsonObject();
			  JsonElement elem = obj.get("recenttracks");
			  System.out.println(elem);
			  //parse Json String with gson Library
			 // List<LastFMTrack> tracks = gson.fromJson(elem.toString(), new TypeToken<List<LastFMTrack>>(){}.getType());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
